﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using MyDBconnection;

namespace CamposInventory
{
    public partial class frmUpdateproduct : Form
    {
        FrmMain parent;
        DatabaseConnection dbconn = new DatabaseConnection();
        
        DataTable tbl = new DataTable();
        
        int curIndex;
        public frmUpdateproduct(FrmMain p)
        {
            parent = p;
            InitializeComponent();
            this.Load += new EventHandler(frmUpdateproduct_Load);
            txtsearchcode.TextChanged += new EventHandler(txtsearchcode_TextChanged);
            dataGridView1.DoubleClick += new EventHandler(dataGridView1_DoubleClick);
            cmbcategory.SelectedIndexChanged += new EventHandler(cmbcategory_SelectedIndexChanged);
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
        }

        void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtprocode.Text != "" && txtbrand.Text != "" && txtsupprice.Text != "" && txtunitcost.Text != "" && txtqty.Text != "")
            {
                dbconn.dbconnection();
                dbconn.UpdatePassword("Update tblproduct set ProductCode='" + this.txtprocode.Text + "', Brand ='" + this.txtbrand.Text + "',Category ='" + this.cmbcategory.Text + "',Description = '" + this.cmbProdescrip.Text + "', SalePrice='" + this.txtunitcost.Text + "',SupplierPrice='" + this.txtsupprice.Text + "',Unit ='" + this.cmbunit.Text + "',Qty='" + this.txtqty.Text + "' Where ProductCode='" + this.txtprocode.Text + "';");
                MessageBox.Show("Update success!");
                dbconn.DBClose();
                txtboxclear();
            }
            else

            {
                MessageBox.Show("Please check the information to update!", "Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error);    
            
            }
        }

        void cmbcategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            productdescription();
        }

        void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            curIndex = dataGridView1.SelectedRows[0].Index;
            txtprocode.Text = dbconn.tbl.Rows[curIndex][1].ToString();
            txtbrand.Text = dbconn.tbl.Rows[curIndex][2].ToString();
            cmbcategory.Text = dbconn.tbl.Rows[curIndex][3].ToString();
            cmbProdescrip.Text = dbconn.tbl.Rows[curIndex][4].ToString();
            txtunitcost.Text = dbconn.tbl.Rows[curIndex][5].ToString();
            txtsupprice.Text = dbconn.tbl.Rows[curIndex][6].ToString();
            cmbunit.Text = dbconn.tbl.Rows[curIndex][7].ToString();
            txtqty.Text = dbconn.tbl.Rows[curIndex][8].ToString() + " "+cmbunit.Text;

        }

        void txtsearchcode_TextChanged(object sender, EventArgs e)
        {
            search();
        }

        void frmUpdateproduct_Load(object sender, EventArgs e)
        {
            cmbcategory.Items.Add("Paint");
            cmbcategory.Items.Add("Concrete");
            cmbcategory.Items.Add("Electrical");
            cmbcategory.Items.Add("Plumbing");
            cmbcategory.Items.Add("Houseware");
            cmbcategory.Items.Add("Homehardware");
            //for qty
            cmbunit.Items.Add("Liters");
            cmbunit.Items.Add("Kg");
            cmbunit.Items.Add("Dozen");
            cmbunit.Items.Add("Metre");
            cmbunit.Items.Add("Yard");
            cmbunit.Items.Add("Grams");
            txtprocode.Enabled = false;
           
        }
        void productdescription()
        {
            switch (cmbcategory.Text)
            {
                case "Paint":
                    cmbProdescrip.Items.Clear();
                    cmbProdescrip.Items.Add("Flat Latex white");
                    cmbProdescrip.Items.Add("Semi-gloss Latex white");
                    cmbProdescrip.Items.Add("Gloss Latex white");
                    break;
                case "Concrete":
                    cmbProdescrip.Items.Clear();
                    cmbProdescrip.Items.Add("Holcim Excel");
                    cmbProdescrip.Items.Add("Holcim WallRight");
                    break;
            }





        }
        void search()
        {
            if (txtsearchcode.Text != "")
            {
                dbconn.dbconnection();
                dbconn.produpdate("SELECT * FROM tblproduct WHERE ProductCode LIKE '%" + txtsearchcode.Text + "%' OR ID LIKE '%" + txtsearchcode.Text + "%'");
                dataGridView1.DataSource = dbconn.tbl;
                dbconn.DBClose();
            }
        }
        void txtboxclear()
        {
            txtprocode.Text = "";
            txtbrand.Text = "";
            txtqty.Text = "";
            txtunitcost.Text = "";
            txtsupprice.Text = "";
            cmbunit.SelectedIndex = 0;
            cmbcategory.SelectedIndex = 0;
            
        }
    }
}
